/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.biblioteca.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mx.its.dsw2.biblioteca.modelo.Libro;

/**
 *
 * @author Maq.8
 */
@Stateless
public class LibroDao extends AbstractDao<Libro> implements LibroDaoLocal {

    @PersistenceContext(unitName = "Biblioteca8E-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public LibroDao() {
        super(Libro.class);
    }

    @Override
    public boolean findDuplicity(Libro book) {
        Query q = em.createNamedQuery("Libro.findByIsbn");
        q.setParameter("isbn", book.getIsbn());
        return !q.getResultList().isEmpty();
    }

    @Override
    public Libro findBookForLend(String claveLibro) {
        Query q = em.createNamedQuery("Libro.findByClave");
        q.setParameter("clave", claveLibro);
        if(!q.getResultList().isEmpty()){
        return (Libro)q.getResultList().get(0);
        }
        return null;
    }
}
